package cn.touna.jss.common.dict;


import cn.touna.jss.common.dict.entity.IntfConfEntity;
import cn.touna.jss.common.util.JssConstant;
import cn.touna.risk.cache.redis.RedisCache;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by wuwc on 2017/08/28.
 */
@Component
public class CreditIntfConfCache {
    private static final Logger logger = LogManager.getLogger(CreditIntfConfCache.class);

    @Autowired
    private RedisCache redisCache;

    /**
     * 获取缓存接口配置
     * @param method
     * @return
     */
    public IntfConfEntity getExistInfConf(String method) {
        String serviceName=splitIntfType(method)[0];
        String methodName=splitIntfType(method)[1];
        if (StringUtils.isAnyBlank(serviceName, methodName)) {
            return null;
        }
        String key = String.format(JssConstant.REDIS_KEY_CREDIT_INTF, serviceName, methodName);
        if(redisCache.exist(key)){
            JSONObject jsonObject = redisCache.get(key);
            logger.info("查到接口:{},配置信息：{}",method,jsonObject);
            return jsonObject.toJavaObject(IntfConfEntity.class);
        }
        logger.warn("此接口:{},未配置",method);
        return null;
    }

    private String[] splitIntfType(String args) {
        if(StringUtils.isNotBlank(args)) {
            String[] arr = args.split("\\.");
            return arr;
        } else {
            return null;
        }
    }

}
